@extends('admin.layout.master')

@section('style')
    <link rel="stylesheet" type="text/css" href="{{ asset('lib/webuploader/0.1.5/webuploader.css') }}" />
@endsection

@section('content')
    <section class="Hui-article-box">
        <nav class="breadcrumb">
            <i class="Hui-iconfont">&#xe67f;</i> 首页
            <span class="c-gray en">&gt;</span> 内容管理
            <span class="c-gray en">&gt;</span> 修改内容
            <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >
                <i class="Hui-iconfont">&#xe68f;</i>
            </a>
        </nav>
        <div class="pd-20">
            <form class="form form-horizontal" method="post" action="">
                {{ csrf_field() }}
                <input type="hidden" name="id" value="{{ $article->id }}">
                <div class="row cl">
                    <label class="form-label col-xs-2 col-sm-1"><span class="c-red">*</span>标题：</label>
                    <div class="formControls col-xs-4 col-sm-5">
                        <input type="text" class="input-text" name="title"
                               value="{{ old('title') ? old('title') : $article->title }}">
                    </div>
                    <div class="col-xs-4 col-sm-5 c-error">{{ $errors->first('title') }}</div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-2 col-sm-1"><span class="c-red">*</span>栏目：</label>
                    <div class="formControls col-xs-4 col-sm-5">
                        <span class="select-box">
                            <select name="category_id" class="select">
                                <option value="">--请选择--</option>
                                @foreach($categorys as $category)
                                    <option value="{{ $category->id }}"
                                    @if(old('category_id'))
                                        {{ old('category_id') == $category->id ? 'selected' : '' }}
                                            @else
                                        {{ $article->category_id == $category->id ? 'selected' : '' }}
                                            @endif
                                    >
                                        @if(count(explode(',', $category->fullpath)) >= 2)
                                            {{ str_repeat('&nbsp;&nbsp;', count(explode(',', $category->fullpath))) . '├&nbsp;' }}
                                        @endif
                                        {{ $category->name }}
                                    </option>
                                @endforeach
                            </select>
                        </span>
                    </div>
                    <div class="col-xs-4 col-sm-5 c-error">{{ $errors->first('category_id') }}</div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-2 col-sm-1">摘要：</label>
                    <div class="formControls col-xs-4 col-sm-5">
                        <input type="text" class="input-text" name="summary"
                               value="{{ old('summary') ? old('summary') : $article->summary }}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-2 col-sm-1">内容：</label>
                    <div class="formControls col-xs-4 col-sm-5">
                        <script id="editor" name="content" type="text/plain" style="width:100%;height:400px;">
                            {!! old('content') ? old('content') : $article->content !!}
                        </script>
                    </div>
                </div>
                <div class="row cl">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-2 col-sm-offset-1">
                        <button class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存并提交</button>
                    </div>
                </div>
            </form>
        </div>
    </section>
@endsection

@section('javascript')
    <script type="text/javascript" src="{{ asset('lib/webuploader/0.1.5/webuploader.js') }}"></script>
    <script type="text/javascript" src="{{ asset('lib/ueditor/1.4.3/ueditor.config.js') }}"></script>
    <script type="text/javascript" src="{{ asset('lib/ueditor/1.4.3/ueditor.all.min.js') }}"> </script>
    <script type="text/javascript" src="{{ asset('lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js') }}"></script>
    <script type="text/javascript">

        var ue = UE.getEditor('editor');

        var uploader = WebUploader.create({
            swf: '{{ asset('lib/webuploader/0.1.5/Uploader.swf') }}',
            server: '{{ asset('lib/webuploader/0.1.5/server/fileupload.php') }}',
            pick: {id: '#picker', multiple: false},
            chunked: true,
            chunkSize: 1024 * 1024,
            formData: {guid: 'yamete'},
            resize: false
        });

        uploader.on( 'fileQueued', function( file ) {
            var $list = $('#thelist');
            $list.append( '<div id="' + file.id + '" class="item">' +
                '<h4 class="info" style="color: #2980B9;">' + file.name + '</h4>' +
                '<p class="state" style="color: #16A085;">等待上传...</p>' +
                '</div>' );
        });

        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                $percent = $li.find('.progress-box .sr-only');
            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<div class="progress-box"><span class="progress-bar radius"><span class="sr-only" style="width:0%"></span></span></div>').appendTo( $li ).find('.sr-only');
            }
            $li.find(".state").text("上传中");
            $percent.css( 'width', percentage * 100 + '%' );
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file ) {
            var fileName = $('#downloadFile').val();
            fileName += file.name + ',';
            $('#downloadFile').val(fileName);
            $( '#'+file.id ).addClass('upload-state-success').find(".state").text("已上传");
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            $( '#'+file.id ).addClass('upload-state-error').find(".state").text("上传出错");
        });

        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress-box').fadeOut();
        });

        $(function () {

            $('#ctlBtn').click(function () {
                uploader.upload();
            });

            $('#factory').change(function () {
                var id = $(this).val();
                $.ajax({
                    headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
                    type: 'POST',
                    url: '{{ url('admin/type/getModel') }}',
                    dataType: 'json',
                    data: {id: id},
                    success: function (data) {
                        if (data.code == '0') {
                            $('#model').html(data.msg);
                            $('#product').html('<option value="">--请选择--</option>');
                        }
                    }
                });
            });

            $('#model').change(function () {
                var id = $(this).val();
                $.ajax({
                    headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
                    type: 'POST',
                    url: '{{ url('admin/type/getModel') }}',
                    dataType: 'json',
                    data: {id: id},
                    success: function (data) {
                        if (data.code == '0') {
                            $('#product').html(data.msg);
                        }
                    }
                });
            });

        });

    </script>
@endsection